package com.agino.jdk8.learn.threadLock.classlock;

/**
 * 线程之间数据没有共享，但是最后的结果是对的
 * todo 检查多线程的实现源码
 * 不同线程申请同一个变量的时候，发生了什么事情
 *
 * @author agino
 * @version 1.0
 * @date 2022/7/22 9:39
 */
public class ClassLock {


    public static void main(String[] args) {
        ClassLock classLock = new ClassLock();
        Thread thread1 = new TestThread(classLock);
        thread1.setName("Thread1");
        Thread thread2 = new TestThread(classLock);
        thread2.setName("Thread2");
        Thread thread3 = new TestThread(classLock);
        thread3.setName("Thread3");
        Thread thread4 = new TestThread(classLock);
        thread4.setName("Thread4");
        Thread thread5 = new TestThread(classLock);
        thread5.setName("Thread5");

        thread1.start();
        thread2.start();
        thread3.start();
        thread4.start();
        thread5.start();
    }

    public static class TestThread extends Thread {

        private ClassLock classLock;

        public TestThread(ClassLock classLock) {
            this.classLock = classLock;
        }

        @Override
        public void run() {
            for (int i = 0; i < 100; i++) {
                classLock.testClassLock(this.getName());
            }
        }
    }

    private int ENTRY_COUNT = 0;

    private void testClassLock(String threadName) {
        ENTRY_COUNT = ENTRY_COUNT + 1;
        System.out.println("线程" + threadName + "正在执行，结果是：" + ENTRY_COUNT);
    }
}
